//
//  SLLaunchViewController.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/18.
//


#import "SLLaunchViewController.h"
#import <YYKit.h>
#import <Masonry.h>
#import "CYLTabBarControllerConfig.h"
#import "OriginData.h"
#import "WiFiInfo.h"

@interface SLLaunchViewController ()
@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) UIView *boradView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) YYLabel *detailLabel;
@property (nonatomic, strong) UIButton *notagreeButton;
@property (nonatomic, strong) UIButton *agreeButton;
@end

@implementation SLLaunchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.bgView];
//    [SLLocationService startLocationWithCompletion:^(CGFloat lat, CGFloat lon) {
//        [WiFiInfo.shared getWiFiInfo:nil];
//    }];
    [RequestCommonParameter idfa];
    BOOL isAgree = [NSUserDefaults.standardUserDefaults boolForKey:@"kAgreement"];
    BOOL isLogin = User.user.token.length > 0;
    if (isAgree || isLogin) {
        self.boradView.hidden = YES;
    }
    UIWindow *window = UIApplication.sharedApplication.keyWindow;
    if (isLogin) {
        [SLRoutePush routeMain];
    } else {
        // 未登录
        window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[NSClassFromString(@"SLLoginViewController") new]];
    }
    [self.view addSubview:self.boradView];
    [self.boradView addSubview:self.titleLabel];
    [self.boradView addSubview:self.detailLabel];
    [self.boradView addSubview:self.notagreeButton];
    [self.boradView addSubview:self.agreeButton];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.boradView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.boradView);
        make.top.equalTo(self.boradView).offset(39);
    }];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.boradView).inset(34);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(17);
    }];
    [self.notagreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.detailLabel.mas_bottom).offset(20);
        make.left.equalTo(self.boradView).inset(35);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).inset(2);
        make.height.mas_equalTo(45);
    }];
    [self.agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.notagreeButton.mas_right).offset(24);
        make.centerY.equalTo(self.notagreeButton);
        make.right.equalTo(self.boradView).inset(35);
        make.width.equalTo(self.notagreeButton);
        make.height.equalTo(self.notagreeButton);
    }];
    
    
    [self.view layoutIfNeeded];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.view.bounds
                                                   byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
                                                         cornerRadii:CGSizeMake(20, 20)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.boradView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.boradView.layer.mask = maskLayer;
    
    
    NSString *text = self.detailLabel.text;
    NSRange range1 = [text rangeOfString:@"Loan Agreement"];
    NSRange range2 = [text rangeOfString:@"Privacy Policy"];
    // 创建富文本
    NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:text];
    //[attrText setColor:UIColorHex(#1B1B1B) range:range1]; // 设置高亮文本颜色
    [attrText setColor:UIColorHex(#999999) range:NSMakeRange(0, text.length)]; // 设置高亮文本颜色
    [attrText setFont:[UIFont systemFontOfSize:14 weight:700] range:range1];
    [attrText setFont:[UIFont systemFontOfSize:14 weight:700] range:range2];
    [attrText setTextHighlightRange:range1 // 设置高亮范围
                              color:UIColorHex(#1B1B1B)
                    backgroundColor:[UIColor clearColor]
                          tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        NSLog(@"点击了高亮文本");
        // 在这里处理点击事件
        [SLRoutePush route:Configuration.LoanAgreementDomain parameter:nil];
    }];
    [attrText setTextHighlightRange:range2 // 设置高亮范围
                              color:UIColorHex(#1B1B1B)
                    backgroundColor:[UIColor clearColor]
                          tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        NSLog(@"点击了高亮文本");
        // 在这里处理点击事件
        [SLRoutePush route:Configuration.PrivacyPolicyDomain parameter:nil];
    }];
    self.detailLabel.attributedText = attrText;
    [self.detailLabel sizeToFit];
    
    [self.detailLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(CGRectGetHeight(self.detailLabel.bounds));
    }];
}



#pragma mark - 点击事件
- (void)agreeButtonAction {
    [NSUserDefaults.standardUserDefaults setBool:YES forKey:@"kAgreement"];
    UIApplication.sharedApplication.keyWindow.rootViewController = [NSClassFromString(@"SLLoginViewController") new];
}
- (void)notagreeButtonAction {
    self.boradView.hidden = YES;
}

- (UIImageView *)bgView {
    if (!_bgView) {
        _bgView = [UIImageView new];
        _bgView.image = [UIImage imageNamed:@"launch"];
        _bgView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _bgView;
}

- (UIView *)boradView {
    if (!_boradView) {
        _boradView = [UIView new];
        _boradView.backgroundColor = UIColor.whiteColor;
    }
    return _boradView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"Welcome to use SulitLoan";
        _titleLabel.textColor = UIColorHex(#1B1B1B);
        _titleLabel.font = [UIFont systemFontOfSize:18 weight:700];
    }
    return _titleLabel;
}
- (YYLabel *)detailLabel {
    if (!_detailLabel) {
        _detailLabel = [YYLabel new];
        _detailLabel.numberOfLines = 0;
        _detailLabel.text = @"Thank you for choosing our app! We understand that the security of your personal information and privacy is of Paramount importance. In order to ensure that your rights and interests are fully protected, please be sure to review all details of Loan Agreement and Privacy Policy before using our products. Only when you fully understand and accept these terms, we recommend that you start to enjoy our products and services. Your confirmation means that you have made a commitment to comply with and accept all relevant provisions.";
        _detailLabel.textColor = UIColorHex(#999999);
        _detailLabel.font = [UIFont systemFontOfSize:14];
    }
    return _detailLabel;
}
- (UIButton *)notagreeButton {
    if (!_notagreeButton) {
        _notagreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_notagreeButton setTitle:@"Not agree" forState:UIControlStateNormal];
        [_notagreeButton setTitleColor:UIColorHex(#1B1B1B) forState:UIControlStateNormal];
        [_notagreeButton setImage:[UIImage imageNamed:@"btn_more_black"] forState:UIControlStateNormal];
        [_notagreeButton jk_setImagePosition:LXMImagePositionRight spacing:10];
        [_notagreeButton setBackgroundColor:UIColorHex(#FFFFFF)];
        _notagreeButton.layer.cornerRadius = 20;
        _notagreeButton.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _notagreeButton.layer.borderWidth = 1;
        [_notagreeButton addTarget:self action:@selector(notagreeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _notagreeButton;
}

- (UIButton *)agreeButton {
    if (!_agreeButton) {
        _agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_agreeButton setTitle:@"Agree" forState:UIControlStateNormal];
        [_agreeButton setTitleColor:UIColorHex(#1B1B1B) forState:UIControlStateNormal];
        [_agreeButton setImage:[UIImage imageNamed:@"btn_more_white"] forState:UIControlStateNormal];
        [_agreeButton jk_setImagePosition:LXMImagePositionRight spacing:10];
        [_agreeButton setBackgroundColor:UIColorHex(#FDC5E0)];
        _agreeButton.layer.cornerRadius = 20;
        _agreeButton.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _agreeButton.layer.borderWidth = 1;
        [_agreeButton addTarget:self action:@selector(agreeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _agreeButton;
}

@end
